class FinanceController < ApplicationController

  before_action :set_menu

  def purchase_payment
    @submenu = :purchase_payment
    @title = '配件采购结算'
    @tasks_grid = initialize_grid(PartPurchase.where(status: 1), per_page: 8, order: 'id', order_direction: 'desc')
    if params.has_key? :payment 
      @part_purchase = PartPurchase.find params[:id]
      @part_purchase.update(payment_method: params[:payment_method], payment_status: 1, pay_datetime: Time.now, pay_total: params[:pay_total])
      redirect_to '/finance/purchase_payment'
    elsif params.has_key? :back
      @part_purchase = PartPurchase.find params[:back]
      @part_purchase.update(payment_method: "", payment_status: 0, pay_datetime: "", pay_total: "")
      redirect_to '/finance/purchase_payment'
    end
  end

  def purchase_payment_pay
    @part_purchase = PartPurchase.find params[:id]
    @payment_method = PaymentMethod.all
  end

  def salse_payment
    @submenu = :salse_payment
    @title = '配件零售结算'
    @tasks_grid = initialize_grid(PartSalse.where(status: 1), per_page: 8, order: 'id', order_direction: 'desc')
    if params.has_key? :payment 
      @part_salse = PartSalse.find params[:id]
      @part_salse.update(payment_method: params[:payment_method], payment_status: 1, pay_datetime: Time.now, pay_total: params[:pay_total])
      redirect_to '/finance/salse_payment'
    elsif params.has_key? :back
      @part_salse = PartSalse.find params[:back]
      @part_salse.update(payment_method: "", payment_status: 0, pay_datetime: "", pay_total: "")
      redirect_to '/finance/salse_payment'
    end
  end

  def salse_payment_pay
    @part_salse = PartSalse.find params[:id]
    @payment_method = PaymentMethod.all
  end

  def maintain_payment
    @submenu = :maintain_payment
    @title = '维修结算'
    @tasks_grid = initialize_grid(OrderMaintain.where(status: 1..4), per_page: 8, order: 'id', order_direction: 'desc')
    if params.has_key? :payment 
      @order_maintain = OrderMaintain.find params[:id]
      @order_maintain.update(payment_method: params[:payment_method], payment_status: 1, pay_datetime: Time.now, pay_total: params[:pay_total])
      redirect_to '/finance/maintain_payment'
    elsif params.has_key? :back
      @order_maintain = OrderMaintain.find params[:back]
      @order_maintain.update(payment_method: "", payment_status: 0, pay_datetime: "", pay_total: "")
      redirect_to '/finance/maintain_payment'
    end
  end

  def maintain_payment_pay
    @order_maintain = OrderMaintain.find params[:id]
    @payment_method = PaymentMethod.all
  end

  def beauty_payment
    @submenu = :beauty_payment
    @title = '美容结算'
    @tasks_grid = initialize_grid(OrderBeauty.where(status: 1..4), per_page: 8, order: 'id', order_direction: 'desc')
    if params.has_key? :payment 
      @order_beauty = OrderBeauty.find params[:id]
      @order_beauty.update(payment_method: params[:payment_method], payment_status: 1, pay_datetime: Time.now, pay_total: params[:pay_total])
      redirect_to '/finance/beauty_payment'
    elsif params.has_key? :back
      @order_beauty = OrderBeauty.find params[:back]
      @order_beauty.update(payment_method: "", payment_status: 0, pay_datetime: "", pay_total: "")
      redirect_to '/finance/beauty_payment'
    end
  end

  def beauty_payment_pay
    @order_beauty = OrderBeauty.find params[:id]
    @payment_method = PaymentMethod.all
  end


  private
  def set_menu
    @mainmenu = :finance
  end

end
